package com.lc.recovery.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppFragment;
import com.lc.recovery.http.glide.GlideApp;
import com.lc.recovery.http.request.MessageListApi;
import com.lc.recovery.http.request.MessageListBean;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.ui.activity.MessageDeatailsActivity;
import com.lc.recovery.util.SPUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MessageListFragment extends AppFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    @BindView(R.id.rv_recoverymessage)
    RecyclerView rvRecoverymessage;
    @BindView(R.id.layout_refresh)
    TwinklingRefreshLayout layoutRefresh;

    // TODO: Rename and change types of parameters
    private String system_id;
    private  int page;
    private String user_type;
    private BaseQuickAdapter<MessageListBean.DataBean, BaseViewHolder> recoveryadapter;
    private List<MessageListBean.DataBean>  dataBeanList=new ArrayList<>();
    private int total;

    public static MessageListFragment newInstance(String system_id) {
        MessageListFragment fragment = new MessageListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, system_id);

        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            system_id = getArguments().getString(ARG_PARAM1);

        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

//        userCouponQuery(system_id,page);

        recoveryadapter = new BaseQuickAdapter<MessageListBean.DataBean, BaseViewHolder>(R.layout.item_business_message, dataBeanList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, MessageListBean.DataBean bean) {
                if (bean.getPicurl().isEmpty()){
                    baseViewHolder.setGone(R.id.img,true);
                }else {
                    baseViewHolder.setVisible(R.id.img,true);
                }

                baseViewHolder.setText(R.id.tv_title, bean.getTitle());
                baseViewHolder.setText(R.id.tv_neirong, bean.getDescript());
                baseViewHolder.setText(R.id.tv_date, bean.getCreate_time());
                if (bean.getIs_read()==0){
                    GlideApp.with(getActivity())
                            .load(bean.getPicurl())
                            .placeholder(R.mipmap.launcher_ic)
                            .error(R.mipmap.launcher_ic)
                            .into((ImageView) baseViewHolder.getView(R.id.img));
                    baseViewHolder.setVisible(R.id.v1,true);
                }else {
                    baseViewHolder.setGone(R.id.v1,true);
                }

            }
        };
        View emptyview = View.inflate(getContext(), R.layout.empty_emptylayout, null);

        recoveryadapter.setEmptyView(emptyview);
        rvRecoverymessage.setAdapter(recoveryadapter);
        recoveryadapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                if (system_id.equals("1")||system_id.equals("2")||system_id.equals("3")){
                    startActivity(new Intent(getContext(), MessageDeatailsActivity.class).putExtra("user_type","1").putExtra("id",dataBeanList.get(position).getId()+""));
                }else {
                    startActivity(new Intent(getContext(), MessageDeatailsActivity.class).putExtra("user_type","2").putExtra("id",dataBeanList.get(position).getId()+""));
                }

            }
        });
        layoutRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                page = 1;
                userCouponQuery(system_id,page);


                refreshLayout.finishRefreshing();

            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                refreshLayout.finishLoadmore();
//                if (!TextUtils.isEmpty(goods_classify_id)) {


                page++;
                if (page < total) {
                    userCouponQueryMore(system_id,page);

                } else {
                    refreshLayout.setEnableLoadmore(false);

                }


//         }
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        userCouponQuery(system_id,page);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_messagelist;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }


    private void userCouponQuery(String type,int page) {
        if (SPUtil.get(getContext(), IntentKey.TYPE,"").equals("1")||SPUtil.get(getContext(), IntentKey.TYPE,"").equals("2")){
            user_type = "1";
        }else if (SPUtil.get(getContext(), IntentKey.TYPE,"").equals("3")){
            user_type="2";
        }else {
            user_type="3";
        }
        EasyHttp.post(this)
                .api(new MessageListApi().setUser_id(SPUtil.get(getContext(), IntentKey.USERID, "")).setType(type).setPage(page+"").setUser_type(user_type)).request(new HttpCallback<MessageListBean>(this) {

            @Override
            public void onSucceed(MessageListBean data) {
                if (data.getCode() == 200) {
                    dataBeanList.clear();
                    dataBeanList.addAll(data.getData());
                    total = data.getCounts();
                    recoveryadapter.setNewData(dataBeanList);
                    recoveryadapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }
    private void userCouponQueryMore(String type,int page) {
        if (SPUtil.get(getContext(), IntentKey.TYPE,"").equals("1")||SPUtil.get(getContext(), IntentKey.TYPE,"").equals("2")){
            user_type = "1";
        }else if (SPUtil.get(getContext(), IntentKey.TYPE,"").equals("3")){
            user_type="2";
        }else {
            user_type="3";
        }
        EasyHttp.post(this)
                .api(new MessageListApi().setUser_id(SPUtil.get(getContext(), IntentKey.USERID, "")).setType(type).setPage(page+"").setUser_type(user_type)).request(new HttpCallback<MessageListBean>(this) {

            @Override
            public void onSucceed(MessageListBean data) {
                if (data.getCode() == 200) {

                    dataBeanList.addAll(data.getData());
                    total = data.getCounts();
                    recoveryadapter.setNewData(dataBeanList);
                    recoveryadapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }
}
